import { defineStore } from "pinia";
import { StoreKeys } from "@/common/enum";
import { getStorage, setStorage } from "@/common/util";
import type { AdminUserType } from "@/common/type";

// 当前用户
export const useAdminUser = defineStore(StoreKeys.adminUser, {
  state: () => {
    const exist = getStorage(StoreKeys.adminUser);
    return {
      adminUser: exist ? JSON.parse(exist) : ({} as AdminUserType),
    };
  },
  actions: {
    setAdminUser(currentUser: AdminUserType) {
      this.adminUser = currentUser;
      setStorage(StoreKeys.adminUser, JSON.stringify(currentUser));
    },
  },
});

// 当前用户token
export const useToken = defineStore(StoreKeys.token, {
  state: () => {
    const exist = getStorage(StoreKeys.token);
    return {
      token: exist || "",
    };
  },
  actions: {
    setToken(newToken: string) {
      this.token = newToken;
      setStorage(StoreKeys.token, newToken);
    },
  },
});
